/**
* Copyright (c) By zengqh.
*
* This program is just for fun or demo, in the hope that it  
* will be useful, you can redistribute it and/or modify freely.
*
* Time: 2013/05/29
* File: enn_timer.cpp
* Blog: http://www.cnblogs.com/zengqh/
**/

#include "enn_timer.h"

namespace enn
{
Time::Time() :
frameNumber_(0),
timeStep_(0.0f),
timerPeriod_(0)
{

}

Time::~Time()
{
	
}

void Time::BeginFrame(float timeStep)
{
	++frameNumber_;
	if (!frameNumber_)
		++frameNumber_;

	timeStep_ = timeStep;
}

void Time::EndFrame()
{

}

void Time::SetTimerPeriod(unsigned mSec)
{

}

float Time::GetElapsedTime()
{
	return elapsedTime_.GetMSec(false) / 1000.0f;
}

unsigned Time::GetSystemTime()
{
#if ENN_PLATFORM == ENN_PLATFORM_WIN32
	unsigned currentTime = timeGetTime();
#else
	struct timeval time;
	gettimeofday(&time, NULL);
	unsigned currentTime = time.tv_sec * 1000 + time.tv_usec / 1000;
#endif

	return currentTime;
}

String Time::GetTimeStamp()
{
	time_t sysTime;
	time(&sysTime);
	const char* dateTime = ctime(&sysTime);
	return String(dateTime);
}

void Time::Sleep(unsigned mSec)
{
#if ENN_PLATFORM == ENN_PLATFORM_WIN32
	::Sleep(mSec);
#else
	usleep(mSec * 1000);
#endif
}

//////////////////////////////////////////////////////////////////////////
Timer::Timer()
{
	Reset();
}

unsigned Timer::GetMSec(bool reset)
{
#if ENN_PLATFORM == ENN_PLATFORM_WIN32
	unsigned currentTime = timeGetTime();
#else
	struct timeval time;
	gettimeofday(&time, NULL);
	unsigned currentTime = time.tv_sec * 1000 + time.tv_usec / 1000;
#endif

	unsigned elapsedTime = currentTime - startTime_;
	if (reset)
		startTime_ = currentTime;

	return elapsedTime;
}

void Timer::Reset()
{
#if ENN_PLATFORM == ENN_PLATFORM_WIN32
	startTime_ = timeGetTime();
#else
	struct timeval time;
	gettimeofday(&time, NULL);
	startTime_ = time.tv_sec * 1000 + time.tv_usec / 1000;
#endif
}

}